<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<html>
<head>
<title>告警规则设置</title>
<meta name="decorator" content="nblank"/>
</head>
<body>
<div class="content container">
    <h2 class="page-title">告警规则设置
        <small>告警规则${not empty param.id?'修改':'添加'}</small>
    </h2>
    <section class="widget widget-tabs widget-table-overflow ">
        <header>
            <ul class="nav nav-tabs">
                <li><a href="${ctx}/biz/eiwRule?officeId=${officebiz.id}">${officebiz.name}设备列表</a></li>
                <li>
                    <a href="${ctx}/biz/eiwRule/list?officeId=${officebiz.id}&sgroupId=${sgroup.id}&itemId=${eitem.id}">${officebiz.name}告警规则设置</a>
                </li>
                <li class="active"><a
                        href="javascript:;">${officebiz.name}设备告警规则${not empty param.id?'修改':'添加'}</a>
                </li>
            </ul>
        </header>
        <div class="body tab-content">
            <form:form id="inputForm" modelAttribute="condition" action="${ctx}/biz/eiwRule/addThroughTemplate" method="post"
                       class="form-horizontal">
                <input type="hidden" id="officeId" name="officeId" value="${officebiz.id}"/>
                <input type="hidden" id="sgroupId" name="sgroupId" value="${sgroup.id}"/>
                <input type="hidden" id="itemId" name="itemId" value="${eitem.id}"/>
                <input type="hidden" id="dataLength" name="dataLength" value="${dataLength}"/>
                <div class="form-group">
                    <label class="control-label col-sm-4"><span class="help-inline"><span style="color:red;">*</span></span>
                        模板:</label>
                    <div class="col-sm-4">
                        <form:select path="spare2" data-width="auto" data-style="btn-primary"
                                     class="selectpicker">
                            <form:option value="" label="请选择"/>
                            <form:options items="${templateList}" itemValue="id" itemLabel="name"/>
                        </form:select>
                    </div>
                </div>
                <div class="form-actions">
                    <div class="text-align-center">
                        <input id="btnCancel" class="btn btn-default" type="button"
                               style="margin-left: -50px;min-width:100px;" value="返 回" onClick="history.go(-1)"/>
                        <input id="btnSubmit" class="btn btn-primary" style="margin-left:50px;min-width:100px;"
                               type="submit" value="保 存"/>&nbsp;
                    </div>
                </div>
            </form:form>
            <div class="widget mg-bottom">
                <div class="list-box" id="inner-content-div">
                    <table id="mainTable" class="table table-striped  table-condensed tree_table overflow-auto">
                        <thead>
                        </thead>
                        <tbody class="table-cor">
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </section>
</div>
<script type="text/javascript">
    $(function () {
        var templateMsg="应用模板规则，系统将清除该设备现存的所有规则，您确定应用该模板吗?";

        $('#spare2').on('changed.bs.select', function (e) {
            if($('#spare2').val()!=''){
                $.ajax({
                    type:"get",
                    url:"${ctx}/biz/eti/listByTemplateId",
                    data:{
                        templateId:$('#spare2').val(),
                    },
                    dataType:"json",
                    success: function (data) {
                        $("#mainTable thead").html("");
                        $("#mainTable tbody").html("");
                        if (data != null) {
                            $("#mainTable thead").append("<tr><th>设备名称</th><th>规则</th><th>时间</th><th>等级</th><th>备注</th></tr>");
                            $.each(data, function (indexColumn, item) {
                                var tds="";
                                tds+="<td>"+item.param.name+"</td>";
                                tds+="<td>"+getDictLabel(${fns:toJson(fns:getDictList('comparison_operator'))}, item.comparisonOperator1);
                                tds+="&nbsp;"+item.operand1;
                                if(item.relationOperator!=''){
                                    tds+="&nbsp;"+getDictLabel(${fns:toJson(fns:getDictList('relation_operator'))}, item.relationOperator);
                                    tds+="&nbsp;"+getDictLabel(${fns:toJson(fns:getDictList('comparison_operator'))}, item.comparisonOperator2);
                                    tds+="&nbsp;"+item.operand2;
                                }
                                tds+="</td>";
                                tds+="<td>"+item.timeBegin+"&nbsp;--&nbsp;"+item.timeEnd+"</td>";
                                tds+="<td>"+getDictLabel(${fns:toJson(fns:getDictList('event_level'))}, item.level)+"</td>";
                                tds+="<td>"+item.remarks+"</td>";
                                $("<tr>"+tds+"</tr>").appendTo("#mainTable tbody");
                            });
                        }
                    }
                });
            }else{
                $("#mainTable thead").html("");
                $("#mainTable tbody").html("");
            }
        });


        $("#inputForm").validate({
            ignore: "",
            focusInvalid: true,
            rules: {
                spare2: {
                    required: true
                }
            },
            messages: {
                "spare2": {
                    required: "必选信息"
                }
            },
            submitHandler: function (form) {
                if($('#dataLength').val()!=0){//已经存在数据
                    bootbox.confirm({
                        size:"middle",
                        title: "系统提示",
                        message:templateMsg,
                        callback: function(result){
                            if(result){
                                $(form).find(":submit").attr("value", "提交中...");
                                $(form).ajaxSubmit({
                                    success: function (result) {
                                        Messenger().post("操作成功!");
                                        pxTrigDlg("${ctx}/biz/eiwRule/list?officeId=${officebiz.id}&sgroupId=${sgroup.id}&itemId=${eitem.id}");
                                    }
                                });
                            }
                        }
                    });
                }else{
                    $(form).find(":submit").attr("value", "提交中...");
                    $(form).ajaxSubmit({
                        success: function (result) {
                            Messenger().post("操作成功!");
                            pxTrigDlg("${ctx}/biz/eiwRule/list?officeId=${officebiz.id}&sgroupId=${sgroup.id}&itemId=${eitem.id}");
                        }
                    });
                }
            },
            errorPlacement: function (error, element) {
                error.appendTo(element.parent());
            }
        });
    });
</script>

</body>
</html>